package com.enn.demo.core.domain.repository.converter;

import com.enn.demo.core.domain.shared.enums.StatusEnum;
import com.enn.demo.core.domain.model.person.Age;
import com.enn.demo.core.domain.model.person.Birthday;
import com.enn.demo.core.domain.model.person.Person;
import com.enn.demo.core.domain.model.person.PersonId;
import com.enn.demo.core.domain.model.user.UserName;
import com.enn.demo.dal.entity.SysPersonDO;

import java.util.Date;

/**
 * 用户Converter
 *
 * @author haoxin
 * @date 2021-02-10
 **/
public class PersonConverter {

    public static Person toPerson(SysPersonDO sysPersonDO) {
        if(sysPersonDO == null) {
            return null;
        }
        return new Person(new PersonId(sysPersonDO.getId()), new UserName(sysPersonDO.getUserName()),new Age(sysPersonDO.getAge()),new Birthday(sysPersonDO.getBirthday()), StatusEnum.getStatusEnum(sysPersonDO.getStatus()));
    }

    public static SysPersonDO fromPerson(Person person) {
        SysPersonDO sysPersonDO = new SysPersonDO();
        sysPersonDO.setId(person.getPersonId()!=null?person.getPersonId().getId():null);
        sysPersonDO.setUserName(person.getUserName()!=null?person.getUserName().getName():"");
        sysPersonDO.setAge(person.getAge()!=null?person.getAge().getAge():null);
        sysPersonDO.setBirthday(person.getBirthday()!=null?person.getBirthday().getBirthday():new Date());
        sysPersonDO.setStatus(person.getStatus()==null?null:person.getStatus().getValue());
        return sysPersonDO;
    }

}
